import { api } from './api'
import type { ApiResponse, PaginatedResponse, PaginationParams } from '../types'

interface Notification {
  id: number
  type: 'COMMENT' | 'LIKE' | 'FOLLOW' | 'SYSTEM'
  title: string
  content: string
  read: boolean
  data?: Record<string, any>
  sender?: {
    id: number
    username: string
    avatar?: string
  }
  recipient: {
    id: number
    username: string
    avatar?: string
  }
  createdAt: string
}

interface CreateNotificationRequest {
  type: Notification['type']
  title: string
  content: string
  data?: Record<string, any>
  recipientId: number
}

interface GetNotificationsParams extends PaginationParams {
  type?: Notification['type']
  read?: boolean
}

export const notificationsApi = api.injectEndpoints({
  endpoints: (builder) => ({
    getNotifications: builder.query<ApiResponse<PaginatedResponse<Notification>>, GetNotificationsParams>({
      query: (params) => ({
        url: '/notifications',
        params,
      }),
      providesTags: (result) =>
        result
          ? [
              ...result.data.content.map(({ id }) => ({ type: 'Notification' as const, id })),
              { type: 'Notification' as const, id: 'LIST' },
            ]
          : [{ type: 'Notification' as const, id: 'LIST' }],
    }),

    getNotification: builder.query<ApiResponse<Notification>, number>({
      query: (id) => `/notifications/${id}`,
      providesTags: (result) =>
        result ? [{ type: 'Notification' as const, id: result.data.id }] : [],
    }),

    createNotification: builder.mutation<ApiResponse<Notification>, CreateNotificationRequest>({
      query: (data) => ({
        url: '/notifications',
        method: 'POST',
        body: data,
      }),
      invalidatesTags: [{ type: 'Notification', id: 'LIST' }],
    }),

    markAsRead: builder.mutation<ApiResponse<void>, number>({
      query: (id) => ({
        url: `/notifications/${id}/read`,
        method: 'PUT',
      }),
      invalidatesTags: (_, __, id) => [{ type: 'Notification', id }],
    }),

    markAllAsRead: builder.mutation<ApiResponse<void>, void>({
      query: () => ({
        url: '/notifications/read-all',
        method: 'PUT',
      }),
      invalidatesTags: [{ type: 'Notification', id: 'LIST' }],
    }),

    deleteNotification: builder.mutation<ApiResponse<void>, number>({
      query: (id) => ({
        url: `/notifications/${id}`,
        method: 'DELETE',
      }),
      invalidatesTags: [{ type: 'Notification', id: 'LIST' }],
    }),

    deleteAllNotifications: builder.mutation<ApiResponse<void>, void>({
      query: () => ({
        url: '/notifications',
        method: 'DELETE',
      }),
      invalidatesTags: [{ type: 'Notification', id: 'LIST' }],
    }),

    getUnreadCount: builder.query<ApiResponse<{ count: number }>, void>({
      query: () => '/notifications/unread-count',
      providesTags: [{ type: 'Notification', id: 'COUNT' }],
    }),
  }),
})

export const {
  useGetNotificationsQuery,
  useGetNotificationQuery,
  useCreateNotificationMutation,
  useMarkAsReadMutation,
  useMarkAllAsReadMutation,
  useDeleteNotificationMutation,
  useDeleteAllNotificationsMutation,
  useGetUnreadCountQuery,
} = notificationsApi 